// scaup - Scala up!
// (c) 2010, Normen Müller, normen.mueller@gmail.com  
// $Id: JNA.scala 125 2010-09-16 13:48:09Z normen.mueller $
package scaup.io.jna

import com.sun.jna.Native
import com.sun.jna.Library
import com.sun.jna.Pointer

/** @author  <a href="mailto:normen.mueller@googlemail.com">Normen M\u00FCller</a>
 */
private[jna] trait ICLibrary extends Library {
  def chmod(filename: String, mode: Int): Int

  def readlink(filename: String, linkname: Pointer, linkNameSize: Int): Int

  def __lxstat64(ver: Int, path: String, stat: Pointer): Int

  def lstat(path: String, stat: Pointer): Int

  def __xstat64(ver: Int, path: String, stat: Pointer): Int

  def stat(path: String, stat: Pointer): Int

  def symlink(targetPath: String, linkPath: String): Int

  def getuid(): Int

  def getgid(): Int
}

/** @author <a href="mailto:normen.mueller@googlemail.com">Normen M\u00FCller</a>
 */
trait JNA {
  import scaup.io.OS

  protected lazy val cLibrary =
    if (OS.isOSX || OS.isLinux)
      (Native.loadLibrary("c", classOf[ICLibrary])).asInstanceOf[ICLibrary]
    else error("JNA not supported OS.")

  lazy val uid = try { cLibrary.getuid() } catch { case _ => -1 }

  lazy val gid = try { cLibrary.getgid() } catch { case _ => -1 }
}
